//
//  HDAccount.swift
//  HuaDaoPeifu
//
//  Created by Erickson on 16/4/7.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit



enum HDLoginType :Int {
//    case NotLogin = 0,Vip,Normal
    case notLogin = 0,group,Vip,individual
}

enum HDUserType:Int {
    case notLogin = 0,group, exclusive, personal
}

let HDAccountHandler = HDAccount.sharedInstance
let kUserType = "userType"
let kLoginType = "loginType"

class HDAccount: NSObject {
    
    static let sharedInstance = HDAccount()
    fileprivate override init(){}

    var token:String?
    
    var phone:String?
    
    var loginType:HDLoginType? = HDLoginType.notLogin
    
    var insuranceno:String?
    
    var uploadImg:UIImage?
    var userName:String?
    var userHeadImage:UIImage?
    let individualVC = HDIndividualCenterViewController()
    
    var userType:HDUserType?// 账户类型，个人/团组/专属
    
    var uid:String?
    
    
    func readUserInfo(){
        if let atkS = UserDefaults.standard.string(forKey: "token") {
            self.token = atkS
            self.phone = UserDefaults.standard.string(forKey: "phone")
            self.userName = UserDefaults.standard.string(forKey: "name")
            self.insuranceno = UserDefaults.standard.string(forKey: "insuranceno")
            self.uid = UserDefaults.standard.string(forKey: "uid")
            
            let logintype = UserDefaults.standard.integer(forKey: kLoginType)
            switch logintype {
            case 0: loginType = .notLogin
            case 1: loginType = .group
            case 2: loginType = .Vip
            case 3: loginType = .individual
            default:break
            }
            
            let usertype = UserDefaults.standard.integer(forKey: kUserType)
            switch usertype {
            case 0: userType = .notLogin
            case 1: userType = .group
            case 2: userType = .exclusive
            case 3: userType = .personal
            default:break
            }
        }else {
            loginType = .notLogin
            userType = .notLogin
        }
        
    }
    /**
     退出登录
     */
    func signOut() {
        token = nil;
        UserDefaults.standard.removeObject(forKey: "token")
        
        saveLoginType(.notLogin)
        
        userName = nil
        UserDefaults.standard.removeObject(forKey: "name")
        
        phone = nil
        UserDefaults.standard.removeObject(forKey: "phone")
        
        userHeadImage = nil
        UserDefaults.standard.removeObject(forKey: "headImage")
        
        insuranceno = nil
        UserDefaults.standard.removeObject(forKey: "insuranceno")
        
        uid = nil
        UserDefaults.standard.removeObject(forKey: "uid")
        
        saveUserIntType(0)
        
        NotificationCenter.default.post(name: Notification.Name(rawValue: HDUserNotification.userSignOut), object: nil)

    }
    
    func saveUserInfo(accountInfo:HDAccountInfo,NeedsPostNotification needsPostNotification:Bool = true) {
        saveUserToken(accountInfo.token)
        saveUserName(accountInfo.name)
        saveUserPhone(accountInfo.phone)
        saveUserID(accountInfo.uid)
        saveUserInsuranceno(accountInfo.insuranceno)
        
        guard let login_type = Int(accountInfo.login_type!),let user_type = Int(accountInfo.user_type!) else {
            return
        }
        
        saveLoginType(HDLoginType(rawValue: login_type)!)
        saveUserIntType(user_type)
        
        if needsPostNotification {
            NotificationCenter.default.post(name: Notification.Name(rawValue: HDUserNotification.userlogin), object: nil)
        }
    }
    
    
    //MARK:    保存用户的 token phone userType insurance
    func saveUserToken(_ token:String?) {
        guard let token = token else {
            return
        }
        self.token = token
        userDefaultsSave(token as AnyObject, forKey: "token")
    }
    func saveUserID(_ uid:String?) {
        guard let uid = uid else {
            return
        }
        self.uid = uid
        userDefaultsSave(uid as AnyObject, forKey: "uid")
    }
    func saveUserPhone(_ phone:String?) {
        guard let phone = phone else {
            return
        }
        self.phone = phone
        userDefaultsSave(phone as AnyObject, forKey: "phone")
    }
    
    
    func saveUserName(_ name:String?) {
        guard let name = name else {
            return
        }
        self.userName = name
        userDefaultsSave(name as AnyObject, forKey: "name")
    }
    func saveUserHeadImage(_ headImage:UIImage){
        self.userHeadImage = headImage
        
        let data = UIImagePNGRepresentation(headImage)
        userDefaultsSave(data as AnyObject, forKey: "headImage")
    }
    
    func saveUserIntType (_ loginType:Int) {
        if loginType == 0 {
            self.userType = .notLogin
        } else if loginType == 1 {
            self.userType = .group
        } else if loginType == 2 {
            self.userType = .exclusive
        } else if loginType == 3 {
            self.userType = .personal
        }
        userDefaultsSave(self.loginType?.rawValue as AnyObject, forKey: kUserType)
    }
    
    func saveLoginType(_ userType:HDLoginType) {
        loginType? = userType
        userDefaultsSave(userType.rawValue as AnyObject, forKey: kLoginType)
        
    }
    
    func saveUserInsuranceno(_ insuranceno:String?) {
        guard let insuranceno = insuranceno else {
            return
        }
        self.insuranceno = insuranceno
        userDefaultsSave(insuranceno as AnyObject, forKey: "insuranceno")
    }
    
    func loadImage() -> (headerImage : UIImage, name : String) {
        
        let user = UserDefaults.standard
        let data = user.object(forKey: "headImage") as? Data
        let name = user.object(forKey: "name") as? String
        
        if data != nil {
            self.userHeadImage = UIImage(data: data!)
            
        } else {
            self.userHeadImage = UIImage(named: "个人中心默认头像")
        }
        
        if name != nil {
            self.userName = name
        } else {
            self.userName = ""
        }
        return(userHeadImage!, userName!)
    }
    
    
    func userDefaultsSave(_ value:AnyObject?,forKey:String){
        UserDefaults.standard.setValue(value, forKey: forKey)
        UserDefaults.standard.synchronize()
        
    }
}


